/**
 * Project  : Looker
 * Package  : net.freestu.looker.common
 * File     : ConfigurationManager.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : Aug 21, 2009
 * Modified : Aug 21, 2009
 * License  : Apache License 2.0 
 */
package net.freestu.looker.common;

import java.io.File;


import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Logger;

/**
 *  配置管理
 *  加载looker和站点配置
 * @author  solosky
 */
public class ConfigurationManager
{
	/**
	 * looker配置
	 */
	private Configuration lookerConfig;
	
	/**
	 * logger
	 */
	private static Logger logger = Logger.getLogger(ConfigurationManager.class);
	
	/**
	 * 配置单例对象
	 */
	private static ConfigurationManager instance; 
	
	/**
	 * 返回配置管理器
	 */
	public static synchronized ConfigurationManager getInstance()
	{
		if(instance==null)
			instance = new ConfigurationManager();
		return instance;
	}
	
	/**
	 * 构造函数
	 */
	private ConfigurationManager()
	{
	}
	
	/**
	 * 返回looker配置
	 */
	public Configuration getLookerConfiguration()
	{
		return this.lookerConfig;
	}
	/**
	 * 载入配置
	 * @throws ConfigurationException 
	 */
	public void loadLookerConfiguration(String conf) throws ConfigurationException
	{
		logger.info("初始化配置...");
		lookerConfig = new XMLConfiguration(conf);
		logger.info("初始化站点配置...");
		File siteDir = new File(lookerConfig.getString("looker.siteConfigDir"));
		if(!siteDir.exists()||siteDir.isFile()){
			//logger.warn("站点目录配置不存在或者是个文件。");
			//return;
			throw new IllegalStateException("站点目录配置不存在或者是个文件。");
		}
		File []  sites = siteDir.listFiles();
		for(int i=0; i<sites.length; i++) {
			//判断是否是目录
			if(sites[i].isDirectory())
				continue;
			Site site = new Site(new XMLConfiguration(sites[i]));
			SiteManager.getInstance().addSite(site);
			logger.info("加载站点配置："+site.getTitle()+" - "+site.getUrl());
		}
	}
	
	public void synchronizeConfiguration()
	{
		int syns = lookerConfig.getList("config.ConfigSynchronizer[@class]").size();
		  ConfigurationSynchronizer synchronizer = null;
		for(int i=0; i<syns; i++) {
			String className = lookerConfig.getString("config.ConfigSynchronizer("+i+")[@class]");
			try {
	            synchronizer = (ConfigurationSynchronizer) Class.forName(className).newInstance();
            } catch (InstantiationException e) {
            	logger.warn("同步器初始化错误 - "+e.getMessage());
            } catch (IllegalAccessException e) {
            	logger.warn("同步器初始化错误 - "+e.getMessage());
            } catch (ClassNotFoundException e) {
            	logger.warn("同步器初始化错误 - "+e.getMessage());
            }
            //s
            logger.info("开始同步 - "+className);
            try {
	            synchronizer.synchronize(SiteManager.getInstance().getSiteList());
            } catch (Exception e) {
            	logger.warn("同步器内部错误 - "+e.getMessage());
            }
		}
           
	}
	
	/**
	 * 
	 * @param args
	 * @throws ConfigurationException
	 */
	
	public static void main(String args[]) throws ConfigurationException
	{
		ConfigurationManager.getInstance().loadLookerConfiguration("./conf/looker.xml");
		ConfigurationManager.getInstance().synchronizeConfiguration();
	}
}
